package com.tencet.servlet;

import com.tencet.entity.AccountTwo;
import com.tencet.entity.OpenAccount;
import com.tencet.entity.Personal;
import com.tencet.entity.User;
import com.tencet.service.Impl.*;
import com.tencet.service.OpenAccountService;
import com.tencet.service.PersonalService;
import com.tencet.utils.CreatAccountUtils;

import javax.servlet.ServletException;
import javax.servlet.annotation.WebServlet;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.io.IOException;
import java.util.UUID;

/**
 * @author Yangjx
 * @version 1.0.0
 * @ClassName OpenAccountServlet.java
 * @Description TODO
 * @createTime 2022年07月20日 19:40
 */
@WebServlet("/openaccount.servlet")
public class OpenAccountServlet extends HttpServlet {
    @Override
    protected void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
        //判断方法走向
        String method = req.getParameter("method");
        if (method.equals("secondAccount")){
            secondAccount(req,resp);
        } else if (method.equals("open")) {
            open(req,resp);
        }
    }

    /**
     * 开户提交时调用
     * @param req
     * @param resp
     */
    private void open(HttpServletRequest req, HttpServletResponse resp) throws IOException {

        //接收前端输入的信息
        String name = req.getParameter("name");
        String idNumber = req.getParameter("idNumber");
        String phone = req.getParameter("phone");
//        req.getSession().setAttribute("phone",phone);
        String secondAccountNumber = req.getParameter("secondAccountNumber");
        String payPassword = req.getParameter("password1");

        //开完户创建二类账户信息
        AccountTwoServiceImpl accountTwoService = new AccountTwoServiceImpl();
        AccountTwo accountTwo = new AccountTwo(UUID.randomUUID()+"",2,0,secondAccountNumber,phone);
        accountTwoService.addBean(accountTwo);

        //TODO 从登录注册获取session中user对象的phone
        User user = (User) req.getSession().getAttribute("user");
        String phone_num = user.getPhone_num();     // TODO 注册时的电话

        // TODO 调用登录的UserServiceImpl，修改registered表中的accounttype(开户状态)为1
        UserServiceImpl userService = new UserServiceImpl();
        int i = userService.update(phone);



        //开户输入的phone与注册电话比较，相同才可注册
        if (phone.equals(phone_num)) {
            //将开户信息封装在开户对象中
            OpenAccount openAccount = new OpenAccount();
            openAccount.setId(UUID.randomUUID()+"");
            openAccount.setAccountType(1);
            openAccount.setName(name);
            openAccount.setIdNumber(idNumber);
            openAccount.setPhone(phone);
            openAccount.setSecondAccountNumber(secondAccountNumber);
            openAccount.setPayPassword(payPassword);
            //将开户对象存入session,以键值对的形式
            req.getSession().setAttribute("openAccount",openAccount);

            //将信息插入个人中心表
            Personal personal1 = new Personal();
            personal1.setP_phone(phone);
            personal1.setP_name(name);
            personal1.setId_card(idNumber);
            personal1.setPassword(payPassword);
            PersonalService personalService = new PersonalServiceImpl();
            //返回是否插入成功
            boolean b = personalService.registPersonal(personal1);
            System.out.println(b);

            OpenAccountService accountService = new OpenAccountServiceImpl();
            //调用OpenAccountServiceImpl的open方法，返回操作数据库成功与否
            boolean result = accountService.open(openAccount);

            if (i>0) {
                //修改注册表开户状态为：已开户
                if (result) {
                    resp.getWriter().print("y");
                } else {
                    resp.getWriter().print("n");
                }
            } else {
                //注册表开户修改失败
                resp.getWriter().print("n");
            }

        } else {
            //开户输入的phone与注册phone不同
            resp.getWriter().print("notphone");
        }
    }

    /**
     * 实名认证,验证信息,生成十位的二类账户号
     * @param req
     * @param resp
     * @throws IOException
     */
    private void secondAccount(HttpServletRequest req, HttpServletResponse resp) throws IOException {
        //实名认证
        String name = req.getParameter("name");
        String idNumber = req.getParameter("idNumber");
        String phone = req.getParameter("phone");

        //调用公安部的接口，验证信息,略

        //生成十位二类账户的账户号
        String secondAccountNumber = CreatAccountUtils.getTel();
        //在session中存入二类账户号，手机号
        req.getSession().setAttribute("secondAccountNumber",secondAccountNumber);


        //账户号传给前端
        resp.getWriter().print(secondAccountNumber);
    }

}
